<?php


namespace come;


class Captcha
{
    function __construct($config=array()){

    }

    /**
     * 生成验证码，并存入session
     */
    static function create(){
        $img = imagecreatetruecolor(60,20);
        $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
        imagefilledrectangle($img,0,0,150,50,imagecolorallocate($img,255,255,255));

        $text = mt_rand(1111,9999);
        imagestring($img,16,4,4,$text,$color);
        header("Content-type:image/png");
        imagepng($img);
        imagedestroy($img);

        Session::set("Captcha",$text);
    }

    /**
     * 验证码检查
     * @param string $code
     * @return bool
     */
    static function check(string $code):bool{
        $captcha = Session::get("Captcha");
        if(empty($captcha)){
            return false;
        }

        if($captcha==$code){
            return true;
        }

        return false;
    }
}